<template>
  <div>
    <van-nav-bar title="登录" />
    <div class="content">
      <van-form @submit="onSubmit">
        <van-field
          v-model="userinfo.userName"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="userinfo.passWord"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >提交</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { login } from '@api/home'
import { Toast } from 'vant'
export default {
  name: 'login',
  data () {
    return {
      userinfo: {
        userName: '',
        passWord: ''
      }
    }
  },
  methods: {
    onSubmit () {
      login(this.userinfo).then(res => {
        if (res.data.status !== 200) {
          this.$toast.fail('登录失败')
        } else {
          this.$toast.success('登录成功')
          window.localStorage.setItem('auth_token', res.data.token)
          this.$router.push(this.$route.query.from)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content{
  margin-top: 20px;
}
</style>
